# -*-coding:utf-8-*-
'''

5.e-data definition:
    > static    > type specifier    > init-dec list > ;
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

3.type definition:
                >>>>>>>>>>>>>>>>>>>>>
    > typedef   > type specifier    >  dec_list > ;
    > struct    >>>>>>>>>>>>>>>>>>>>>
    > enum      >>>>>>>>>>>>>>>>>>>>>

16.init-dec list:
    > declarator    >>>>>>>>>>>>>>>>>>>>>>
                    > = > initializer   >
    < ,                                 <

9.declarator:
    > identifier                                    >
    > ( > declarator    > )                         >
    > * > declarator                                >
    > declarator    > ( > )                         >
    > declarator    > [ >>>>>>>>>>>>>>>>>>>>>>> ]   >
                        > constant expression >

'''
import model
from c_code_base_analyzer import print_comment
from c_code_base_analyzer import c_code_reader


class e_data_definition_anlyzer(c_code_reader):
    def e_data_definition_anlyze(self, line_left):
        if self.e_data_is_found is True:
            if not self.is_in_function:
                para = None
                if line_left.find(',') > 0 and line_left.find(';') > 0:
                    for dec in line_left[:line_left.find(';')].split(','):
                        if dec != "":
                            if dec.find('=') < 0:
                                para = model.para_info(
                                    name=dec.strip(),
                                    type_name=self.e_data_type_tmp)
                            else:
                                value_tmp = None
                                try:
                                    value_tmp = eval(dec[dec.find('=') + 1:])
                                except Exception:
                                    print_comment('do not care')
                                para = model.para_info(
                                    name=dec[:dec.find('=')].strip(),
                                    type_name=self.e_data_type_tmp,
                                    init_value=value_tmp)
                        if line_left.find('=') > 0 and line_left.find(
                                '=') < line_left.find(','):
                            break
                elif line_left.find('=') < 0 and line_left.find(';') > 0:
                    para = model.para_info(
                        name=line_left[:line_left.find(';')].strip(),
                        type_name=self.e_data_type_tmp)
                elif line_left.find('=') > 0:
                    value_tmp = None
                    if line_left.find(';') > 0:
                        try:
                            value_tmp = eval(line_left[line_left.find('=') +
                                                       1:line_left.find(';')])
                        except Exception:
                            print_comment('do not care')
                    else:
                        self.no_use_init_flag = True
                    para = model.para_info(
                        name=line_left[:line_left.find('=')].strip(),
                        type_name=self.e_data_type_tmp,
                        init_value=value_tmp)
                else:
                    print_comment(
                        'no support too complex e-data definition ,sorry.')
                    self.crashed = True
                    return ""
                self.module_info.para_list.append(para)

                line_left = ""
        self.e_data_is_found = False
        self.e_data_type_tmp = ""
        return line_left
